package org.jeecg.modules.erp.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.erp.goods.entity.ErpGoods;
import org.jeecg.modules.erp.goods.entity.ErpGoodsPackage;
import org.jeecg.modules.erp.goods.mapper.ErpGoodsMapper;
import org.jeecg.modules.erp.goods.mapper.ErpGoodsPackageMapper;
import org.jeecg.modules.erp.goods.service.IErpGoodsPackageService;
import org.jeecg.modules.erp.goods.service.IErpGoodsService;
import org.jeecg.modules.erp.goods.vo.ErpGoodsPackageVo;
import org.jeecg.modules.erp.goods.vo.ErpGoodsPage;
import org.jeecg.modules.erp.goods.vo.ErpGoodsVo;
import org.jeecg.modules.erp.goods.vo.ErpMateralSupplierVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 物料基础信息
 * @Author: jeecg-boot
 * @Date: 2024-05-21
 * @Version: V1.0
 */
@Service
public class ErpGoodsServiceImpl extends ServiceImpl<ErpGoodsMapper, ErpGoods> implements IErpGoodsService {

    @Autowired
    private ErpGoodsMapper erpGoodsMapper;
    @Autowired
    private ErpGoodsPackageMapper erpGoodsPackageMapper;
    @Autowired
    private IErpGoodsPackageService erpGoodsPackageService;

    @Override
    public IPage<ErpGoodsPage> goodsPage(Page<ErpGoods> page, ErpGoods model) {
        return getBaseMapper().goodsPages(page, model);
    }

    @Override
    public IPage<ErpGoodsPage> goodsSupplierPage(Page<ErpMateralSupplierVo> page, ErpMateralSupplierVo model){
        return this.getBaseMapper().goodsPagesList(page,model);
    }

    @Override
    public List<ErpGoodsPage> goodsSupplierList(ErpMateralSupplierVo model){
        return this.getBaseMapper().goodsSupplierList(model);
    }

    @Override
    public List<ErpGoods> goodsByIds(List<String> ids) {
        return this.listByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(ErpGoods erpGoods, List<ErpGoodsPackage> erpGoodsPackageList) {
        erpGoodsMapper.insert(erpGoods);
        if(erpGoodsPackageList!=null && erpGoodsPackageList.size()>0) {
            for(ErpGoodsPackage entity:erpGoodsPackageList) {
                //外键设置
                entity.setGoodsId(erpGoods.getId());
                erpGoodsPackageMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(ErpGoods erpGoods,List<ErpGoodsPackage> erpGoodsPackageList) {
        erpGoodsMapper.updateById(erpGoods);

        //1.先删除子表数据
        erpGoodsPackageMapper.deleteByMainId(erpGoods.getId());

        //2.子表数据重新插入
        if(erpGoodsPackageList!=null && erpGoodsPackageList.size()>0) {
            for(ErpGoodsPackage entity:erpGoodsPackageList) {
                //外键设置
                entity.setGoodsId(erpGoods.getId());
                erpGoodsPackageMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        erpGoodsPackageMapper.deleteByMainId(id);
        erpGoodsMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            erpGoodsPackageMapper.deleteByMainId(id.toString());
            erpGoodsMapper.deleteById(id);
        }
    }

    @Override
    public List<ErpGoodsVo> queryGoodsAndFormula(List<String> ids) {
        String[] idArray = ids.stream().toArray(String[]::new);
        List<ErpGoodsVo>  res = erpGoodsMapper.queryGoodsAndFormula(idArray);
        for(ErpGoodsVo erpGoodsVo: res){
            List<ErpGoodsPackageVo> erpGoodsPackageList = erpGoodsPackageService.selectByMainId(erpGoodsVo.getId());
            erpGoodsVo.setPackageVos(erpGoodsPackageList);
        }
        return res;
    }

    @Override
    public void correlationFormula(ErpGoods erpGoods) {
        erpGoodsMapper.correlationFormula(erpGoods);
    }

    @Override
    public List<ErpGoodsVo> queryByReviewId(String id) {
        List<ErpGoodsVo>  res = erpGoodsMapper.queryByReviewId(id);
        for(ErpGoodsVo erpGoodsVo: res){
            List<ErpGoodsPackageVo> erpGoodsPackageList = erpGoodsPackageService.selectByReviewId(id,erpGoodsVo.getId());
            erpGoodsVo.setPackageVos(erpGoodsPackageList);
        }
        return res;
    }
}
